package com.linguochao.rabbitmq.producer;

import com.linguochao.rabbitmq.config.RabbitBatchConfig;
import com.linguochao.rabbitmq.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author linguochao
 * @Description rabbitmq生产者
 * @Date 2020/7/15 18:09
 */
@Service
public class RabbitProducer {
    @Autowired
    private RabbitMessagingTemplate rabbitMessagingTemplate;

    /**
     * rabbitmq 直接模式--发送短信
     *
     * @param mobile
     */
    public void sendsms(String mobile) {
        Map msg = new HashMap();
        msg.put("mobile", mobile);
        msg.put("code", "123456");
        rabbitMessagingTemplate.convertAndSend(RabbitMQConfig.QUEUE_INFORM_SMS, msg);
        System.out.println("mq发送短信成功");
    }

    /**
     * rabbitmq 分列模式--添加用户
     *
     * @param user
     */
    public void adduser(String user) {
        rabbitMessagingTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_DATA, "", user);
        System.out.println("添加用户成功:" + user);
    }

    /**
     * rabbitmq 主题模式-- qq 微信消息
     *
     * @param msg
     */
    public void qqwx(String msg) {
        rabbitMessagingTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_TOPIC, "qq.log", msg);
        rabbitMessagingTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_TOPIC, "wx.log", msg);
        System.out.println("推送qq,微信消息:" + msg);
    }



    public void sendBath(String msg) {
        for (int i = 0; i < 20; i++) {
            rabbitMessagingTemplate.convertAndSend(RabbitBatchConfig.BATCH_EXCHANGE_NAME,RabbitBatchConfig.BATCH_QUEUE_ROUTING_KEY, msg + i);
        }
    }


    /**
     * 死信队列消息的超时时间：实现延迟投递
     */
    @Value("${spring.rabbitmq.batchQueue}")
    private String[] queueList;
    public void sendBathHash(String msg) {
        for (int i = 0; i < 20; i++) {
            int hash = i % queueList.length;
            rabbitMessagingTemplate.convertAndSend(RabbitBatchConfig.BATCH_EXCHANGE_NAME,queueList[hash], msg + i);
        }
    }


}
